package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.article.IArticleClient;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserRealnameDto;
import com.heima.model.user.dtos.CommentUserDto;
import com.heima.model.user.dtos.PagingDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @创建人: stone
 * @描述: 用户审核
 */
@Service
@Transactional
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper userRealnameMapper;

    @Autowired
    private ApUserMapper userMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private IArticleClient articleClient;


    /**
     * 查询用户列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getUserRealnameList(ApUserRealnameDto dto) {

        //参数校验
        if (dto.getPage() == null || dto.getPage() <= 0) {
            dto.setPage(1);
        }
        if (dto.getSize() == null || dto.getSize() <= 0 || dto.getSize() > 50) {
            dto.setSize(10);
        }

        //参数审核
        LambdaQueryWrapper<ApUserRealname> QueryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            QueryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }

        //创建分页器
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        page = userRealnameMapper.selectPage(page, QueryWrapper);

        //返回数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        log.info("查询的分页数据:" + responseResult);

        return responseResult;
    }

    /**
     * 用户审核失败接口
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(ApUserRealnameDto dto) {
        //参数校验
        if (dto.getId() == null) {
            return ResponseResult.errorResult(404, "Not Found");
        }
        ApUserRealname UserRealname = getOne(Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getId, dto.getId()));
        if (UserRealname == null) {
            return ResponseResult.errorResult(404, "Not Found");
        }
        if (UserRealname.getStatus() == 2 || UserRealname.getStatus() == 9) {
            return ResponseResult.errorResult(403, "Forbidden");
        }
        //更改信息
        UserRealname.setStatus(2);
        UserRealname.setReason(dto.getMsg());
        UserRealname.setUpdatedTime(new Date());
        updateById(UserRealname);

        return ResponseResult.okResult("ok");
    }

    /**
     * 用户审核成功接口
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(ApUserRealnameDto dto) {
        //参数校验
        if (dto.getId() == null) {
            return ResponseResult.errorResult(404, "Not Found");
        }
        ApUserRealname UserRealname = getOne(Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getId, dto.getId()));
        if (UserRealname == null) {
            return ResponseResult.errorResult(404, "Not Found");
        }
        //更改信息
        UserRealname.setStatus(9);
        UserRealname.setReason(dto.getMsg());
        UserRealname.setUpdatedTime(new Date());
        updateById(UserRealname);

        //创建文章作者信息
        Integer userId = UserRealname.getUserId();
        ApAuthor apAuthor = new ApAuthor();
        //设置属性
        apAuthor.setUserId(userId);
        apAuthor.setName(UserRealname.getName());
        apAuthor.setType(2);
        apAuthor.setCreatedTime(new Date());

        ResponseResult register = articleClient.register(apAuthor);
        register.getData();

        if (register.getData() == null) {
            return ResponseResult.errorResult(501, "账号创建失败");
        }
        //创建自媒体账户
        ApUser apUser = userMapper.selectById(userId);
        WmUser wmUser = new WmUser();
        wmUser = WmUser.init(wmUser, apUser);
        wmUser.setApAuthorId((Integer) register.getData());
        wmUser.setStatus(9);
        wmUser.setType(0);
        wmUser.setCreatedTime(new Date());
        ResponseResult responseResult = iWemediaClient.wmRegister(wmUser);
        if (responseResult.getData() == null) {
            return ResponseResult.errorResult(501, "账号创建失败");
        }

        //设置文章自媒体账户
        apAuthor.setWmUserId(wmUser.getId());
        apAuthor.setId((Integer) register.getData());
        articleClient.register(apAuthor);

        return ResponseResult.okResult("ok");
    }

    /**
     * 查询当前用户
     *
     * @param dto
     */
    @Override
    public ResponseResult getUser(CommentUserDto dto) {
        ApUser apUser = userMapper.selectById(dto.getId());
        dto.setName(apUser.getName());
        dto.setImage(apUser.getImage());
        return ResponseResult.okResult(dto);
    }
}
